package com.thanh.icook;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.TabSpec;

public class QuanLyActivity extends IcookActivity {
	public static int idcate;
	Dialog dialog;
	private ProgressDialog progressDialog;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_quanly);
		settab();
		getCateDish();
        getPeople();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        getMenuInflater().inflate(R.menu.listdish, menu);
        menu.findItem(R.id.menu_item_adddish).setIntent(new Intent(this, MainmenuActivity.class));
        menu.findItem(R.id.menu_item_addpeople).setIntent(new Intent(this, MainmenuActivity.class));
        return true;
    }
    private void settab() {
	    
		//Khoi tao Tabhost
        TabHost host = (TabHost) findViewById(R.id.quanly_tabhost);
        host.setup();
        //Tab mon an
        TabSpec allScoresTab = host.newTabSpec("tabdish");
        allScoresTab.setIndicator(getResources().getString(R.string.tab_dish), getResources().getDrawable(
        				R.drawable.mana_dish));
        allScoresTab.setContent(R.id.tab1);
        //getCateDish();     
        host.addTab(allScoresTab);
        //Tab nguoi an
        TabSpec friendScoresTab = host.newTabSpec("tabpeople");
        friendScoresTab.setIndicator(getResources().getString(R.string.tab_people), 
        		getResources().getDrawable(R.drawable.mana_people));
        friendScoresTab.setContent(R.id.tab2);  
        //getPeople();
        host.addTab(friendScoresTab);
        //Chon tab lam mac dinh
        host.setCurrentTabByTag("tabweek");
        
	}
    private void getCateDish(){   	
   	
    	final GridviewAdapter_People mAdapter;
    	ArrayList<String> CateDishText;
    	ArrayList<Bitmap> CateDishImage;
    	GridView gV_Catedish;
    	final Creadfile read = new Creadfile();
        String jsonCatedish = read.readJsonURL("http://icook.dinhchithanh.com/getcatedish.php");
        CateDishText = new ArrayList<String>();
        CateDishImage = new ArrayList<Bitmap>();
		JSONArray entries;
		try {
			entries = new JSONArray(jsonCatedish);
			for(int i=0;i<entries.length();i++){
				JSONObject post = entries.getJSONObject(i);
				CateDishText.add(post.getString("name"));
				//ID.add(post.getString("id"));
				Bitmap img = read.DownloadImage(post.getString("option"));
				CateDishImage.add(img);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        // prepared arraylist and passed it to the Adapter class
        mAdapter = new GridviewAdapter_People(this,CateDishText, CateDishImage);
        
        
        // Set custom adapter to gridview
        gV_Catedish = (GridView) findViewById(R.id.gridView1);
        gV_Catedish.setAdapter(mAdapter);
        // Implement On Item click listener
        gV_Catedish.setOnItemClickListener(new OnItemClickListener() 
        {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, final int position,
					long arg3) {
				
				progressDialog = ProgressDialog.show(QuanLyActivity.this, "", 
						getResources().getString(R.string.loading), true);
				new Thread(){
					public void run() {
						idcate = position+1;
						startActivity(new Intent(QuanLyActivity.this, ListDishActivity.class));
						progressDialog.dismiss();
					}
				}.start();
				
			}
		});
     
    }  
    private int num_member = 0;
    private int SumEnergyFamily = 0;
    private void getPeople(){
   	 final GridviewAdapter_People mAdapter;
   	 ArrayList<String> PeopleName;
   	 final ArrayList<Bitmap> PeopleImage;
   	 final ArrayList<String> PeopleSex = new ArrayList<String>();
   	 final ArrayList<String> PeopleAge = new ArrayList<String>();
   	 final ArrayList<Integer> PeopleEnergy = new ArrayList<Integer>();
   	 GridView gV_Catedish;
   	Creadfile read = new Creadfile();
       String jsonCatedish = read.readJsonURL("http://icook.dinhchithanh.com/getpeople.php");
       PeopleName = new ArrayList<String>();
       PeopleImage = new ArrayList<Bitmap>();
       JSONArray entries;
		try {
			entries = new JSONArray(jsonCatedish);
			for(int i=0;i<entries.length();i++){
				JSONObject post = entries.getJSONObject(i);
				PeopleName.add(post.getString("name"));
				String sex = post.getString("sex");
				PeopleSex.add(sex);
				Bitmap img = read.DownloadImage(post.getString("image"));
				PeopleImage.add(img);
				String age = post.getString("age");
				PeopleAge.add(age);
				//Tinh nang luong hang ngay cho moi nguoi
				String str_energy = read.readJsonURL("http://icook.dinhchithanh.com/getenergy.php?sex="+sex+"&age=" + age);
				JSONArray jsr_energy = new JSONArray(str_energy);
				for (int j = 0; j < jsr_energy.length(); j++) {
					JSONObject jso_energy = jsr_energy.getJSONObject(j);
					Integer calorie = (Integer) jso_energy.getInt("calorie");
					SumEnergyFamily += calorie;
					PeopleEnergy.add(calorie);	
				}
				num_member++;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       
       // prepared arraylist and passed it to the Adapter class
       mAdapter = new GridviewAdapter_People(this,PeopleName, PeopleImage);
       
       
       // Set custom adapter to gridview
       gV_Catedish = (GridView) findViewById(R.id.gridView2);
       gV_Catedish.setAdapter(mAdapter);
       
       // Implement On Item click listener
       gV_Catedish.setOnItemClickListener(new OnItemClickListener() 
       {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				//Toast.makeText(QuanLyActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
				dialog = new Dialog(QuanLyActivity.this);
				dialog.setContentView(R.layout.categorydialog);
				 dialog.setCancelable(true);
				 TextView text = (TextView) dialog.findViewById(R.id.text1);
				 String gioitinh;
				 if(PeopleSex.get(position).equals("0")) gioitinh= getResources().getString(R.string.lable_people_nam);
				 	else gioitinh = getResources().getString(R.string.lable_people_nu);
				 text.setText(mAdapter.getItem(position)+ "\n" 
						 + getResources().getString(R.string.lable_people_age) + ": " + PeopleAge.get(position) +"\n"
						 + getResources().getString(R.string.lable_people_sex) + ": " + gioitinh + "\n"
						 + getResources().getString(R.string.lable_people_energy) + ": " + PeopleEnergy.get(position));
				 ImageView imgthum = (ImageView) dialog.findViewById(R.id.dialog_imgthum);
				 imgthum.setImageBitmap(PeopleImage.get(position));
				 
				 ImageView imgclose = (ImageView)dialog.findViewById(R.id.close);
				 
				 imgclose.setOnClickListener(new OnClickListener() {					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						//finish();
						dialog.dismiss();
					}
				});
				 dialog.show();
			}
		});
   }
    public int  getNumMember() {
		return num_member;
	}
    public int getSumEnergyFamily () {
		return SumEnergyFamily;
	}
}
